<?php
namespace app\smallapp\controller;
use app\common\controller\BaseApi;
/*小程序接口--pt端*/
class PT extends BaseApi{
	function _initialize() {
		if(!request()->isPost()){
			return $this->api_ajax_error("","非法访问");
		}
		$time=input("time/d","");
		$sign=input("sign/s","");
		if(input("debug")!=1){ 
			if(($time+300)<time()||($time-300)>time()){//接口调用时间操过5分钟
				return $this->api_ajax_error("","链接已经过期");
			}
			if($sign!=md5("ocmcom_pt_".$time)){
				return $this->api_ajax_error("","签名错误！");
			}	
		}
    }
	//////////////////////////////////////////////////////////////////////////////////////
	/**
	 * $this->ajax_api_success(data,msg);
	 * $this->ajax_api_error(data,msg);
	 * */
	//////////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * 根据电话号码获取店铺
	 * */
	function GetShopList(){
		$mobile=input("mobile");
		if(strlen($mobile)!=11){
			return $this->api_ajax_error("","手机号码格式错误");
		}
		$list=db("GymEmployee")->alias("a")->field("b.id,b.shop_name")->join([["m_shop b","a.shop_id=b.id","left"]])->where("a.mobile='$mobile' and a.type=3")->select();
		if(empty($list)){
			return $this->api_ajax_error([],"没有更多的数据");
		}else{
			return $this->api_ajax_success($list);
		}
	}	
	/**
	 * 登录
	 * http://smartgym.com/smallapp/PT/Login
	 * */
	function Login(){
		$moblie=input("mobile/s");
		$shop_id=input("shop_id/d",0);
		$psw=input("psw/s");
		if(empty($shop_id)){
			return $this->api_ajax_error("","shop_id 为空");
		}
		if(strlen($moblie)!=11){
			return $this->api_ajax_error("","手机号码格式错误！");
		}
		if(strlen($psw)<6){
			return $this->api_ajax_error("","密码长度不足6位！");
		}
		$model=db("GymEmployee")->where(" mobile='$moblie' and shop_id=$shop_id and  psw='$psw' and type=3 ")->find();
		if(empty($model)){
			return $this->api_ajax_error("","账号或密码错误！");
		}else{
			if($model['web_open_up']!=1){
				return $this->api_ajax_error("","您暂无此权限");
			}
			return $this->api_ajax_success(
					array(
						"pt_id"=>$model['id'],
						"pt_name"=>$model["name"],
						"shop_id"=>$model["shop_id"],
					));
		}
	}
	/*
	 * 获取短信验证码
	 * http://smartgym.com/smallapp/PT/GetCode
	 * */
	function GetCode(){
		$mobile=input("mobile/s");
		$model=db("GymEmployee")->where("mobile='$mobile' and type=3")->find();
		if(empty($model)){
			return $this->api_ajax_error("","pt不存在！");
		}else{
			if($model['web_open_up']!=1){
				return $this->api_ajax_error("","您暂无此权限");
			}
			if(empty(cache("$mobile"))){
				$result=send_msg_code($mobile);
				if($result['flag']){
					cache("$mobile",$result['data'],60);
					return $this->api_ajax_success("","成功");
				}else{
					return $this->api_ajax_error($result['data'],$result['msg']);
				}
			}else{
				return $this->api_ajax_error("","操作频繁，稍后再试！");
			}
		}
	} 
	/**
	 * 修改密码 
	 *  http://smartgym.com/smallapp/PT/ModifyPsw
	 */
	function ModifyPsw(){
		$mobile=input("mobile/s");
		$new_psw=input("new_psw/s");
		$old_psw=input("old_psw/s");
		$shop_id=input("shop_id/d");
		if(strlen(trim($new_psw))<6){
			return $this->api_ajax_error("","新密码长度必须大于6位");
		}
		if(empty($shop_id)){
			return $this->api_ajax_error("","店铺id 为空");
		}
		$id=db("GymEmployee")->where("mobile='$mobile' and type=3 and psw='$old_psw' and shop_id=$shop_id ")->value("id");
		if(empty($id)){
			return $this->api_ajax_error("","pt账号或密码错误");
		}else{
			db("GymEmployee")->where("id=$id")->setField(array("psw"=>$new_psw));
			return $this->api_ajax_success();
		}
	}
	/**
	 * 修改密码 
	 *  http://smartgym.com/smallapp/PT/FindPsw
	 */
	function FindPsw(){
		$mobile=input("mobile/s");
		$new_psw=input("psw/s");
		$shop_id=input("shop_id/d");
		$code=input("code","xxxx");
		if(strlen(trim($new_psw))<6){
			return $this->api_ajax_error("","新密码长度必须大于6位");
		}
		if(empty($shop_id)){
			return $this->api_ajax_error("","店铺id为空");
		}
		if($code!=cache("$mobile")){
			return $this->api_ajax_error("","验证码错误或过期");
		}
		
		$id=db("GymEmployee")->where("mobile='$mobile' and type=3 and shop_id=$shop_id")->value("id");
		if(empty($id)){
			return $this->api_ajax_error("","pt账号不存在");
		}else{
			db("GymEmployee")->where("id=$id")->setField(array("psw"=>$new_psw));
			return $this->api_ajax_success("");
		}
	}
	
	/**
	 * 首页- 统计数据
	 * */
	function IndexStatistic(){
		$pt_id=input("pt_id");
		if(empty($pt_id)){
			return $this->api_ajax_error("","参数错误！");
		}
		$year_month = date("Ym", time());
		$data["performance_day"]=db("GymPtOrder")->where("pt_id=$pt_id and TO_DAYS(create_time)=TO_DAYS(NOW())")->sum("price");
        $data["performance_month"]= db("gym_employee_achievement")->where(array("year_month" => $year_month, "employee_id" => $pt_id))->value("achievement");
        if (empty($data["performance_month"])) {
            $data["performance_month"] = 0;
        }
		$data["tobegin"]=db("GymPtFollow")->where("pt_id=$pt_id and type=2 and TO_DAYS(order_date)=TO_DAYS(CURDATE()) and order_time>date_format(now(),'%H:%i') and order_time<date_format(date_add(now(), INTERVAL +3 HOUR),'%H:%i') ")->count(1);
		$data["longtimego"]=db("GymPtOrder")->where("pt_id=$pt_id and last_course_time is not  null and  (TO_DAYS(create_time)-TO_DAYS(last_course_time)>45)")->count(1);
		$data['ordercount']=db("GymPtFollow")->where("pt_id=$pt_id and type=2 and order_time is not null")->count(1);
		$data['overdue']=db("GymPtOrder")->where("pt_id=$pt_id and ((TO_DAYS(end_date)-TO_DAYS(CURDATE()) between 0 and 30) or surplus<2)")->count(1);
		return $this->api_ajax_success($data);
	}
	
	/*
	 * 首页- 数据查询
	 * */
	function Index(){
		$start_date=input("start_date","2010-10-10");
		$end_date=input("end_date","2099-10-10");
		$type=input("type/d");
		$start_month=input("start_month",date("Y-m"));
		$pt_id=input("pt_id/d");
		$page=input("page/d",1);
		$field="b.name,case when b.sex=1 then '男' else '女' end as sex ,b.mobile,a.id as member_id,a.pt_id,a.id as pt_order_id,a.price,a.course_name";
		if($type==1){//按照时间端查询
			if(empty($start_date)||empty($end_date)){
				return $this->api_ajax_error("","参数错误1");
			}
			$list=db("GymPtOrder")->alias("a")->field($field)->join([["m_gym_member b","a.member_id=b.id","left"]])->where("a.pt_id=$pt_id and TO_DAYS(a.create_time)>= TO_DAYS('$start_date') AND TO_DAYS(a.create_time)<= TO_DAYS('$end_date')")->page($page,10)->select();
			$total_price=db("GymPtOrder")->where("pt_id=$pt_id and TO_DAYS(create_time)>= TO_DAYS('$start_date') AND TO_DAYS(create_time)<= TO_DAYS('$end_date')")->sum("price");
		}else if($type==2){// 按照月查询
			if(empty($start_month)){
				return $this->api_ajax_error("","参数错误2");
			}
			$list=db("GymPtOrder")->alias("a")->field($field)->join([["m_gym_member b","a.member_id=b.id","left"]])->where("a.pt_id=$pt_id and date_format(a.create_time,'%Y-%m')='$start_month'")->page($page,10)->select();

			$year_month = (int)str_replace("-", "", $start_month);
			$total_price = db("gym_employee_achievement")->where(array("year_month" => $year_month, "employee_id" => $pt_id))->value("achievement");
		}else{//
			return $this->api_ajax_error("","参数错误3");
		}
		if(empty($list)){
			return $this->api_ajax_success(["total_price"=>0,"list"=>[]]);
		}else{
			return $this->api_ajax_success(["total_price"=>$total_price,"list"=>$list]);
		}
	}
	
	/**
	 * 即将开始
	 * */
	function ToBegin(){
		$pt_id=input("pt_id/d");
		if(empty($pt_id)){
			return $this->api_ajax_error("","参数错误");
		}
		$field="a.id as order_id,a.pt_order_id,a.course_count,a.order_time,a.member_id,b.course_name,c.name as member_name";
		$list=db("GymPtFollow")->alias("a")->field($field)->join([["m_gym_pt_order b","a.pt_order_id=b.id","left"],["m_gym_member c","a.member_id=c.id","left"]])->where("a.pt_id=$pt_id and a.type=2 and TO_DAYS(a.order_date)=TO_DAYS(CURDATE()) and a.order_time>date_format(now(),'%H:%i') and a.order_time<date_format(date_add(now(), INTERVAL +3 HOUR),'%H:%i') ")->select();
		if(empty($list)){
			return $this->api_ajax_error("","抱歉没有更多的数据。。。");
		}else{
			return $this->api_ajax_success($list);
		}
	}
	/**
	 * 私教管理--跟进会员
	 * */
	function MemberFollowList(){
		$pt_id=input("pt_id");
		$search_day=input("search_day");
		$keywords=input("keywords");
		$page=input("page/d",1);
		
		$search_day=input("search_day","");
		if(!empty($search_day)){
			$search_day=" and date_format(a.follow_time, '%Y-%m-%d')='$search_day'";
		}
		
		$field="a.id as order_id,a.pt_order_id,a.course_count,a.order_time,a.member_id,a.course_status,b.course_name,b.follow_count,date_format(b.last_follow_time,'%Y-%m-%d') as last_follow_time,date_format(b.last_course_time,'%Y-%m-%d') as  last_course_time,b.course_count,case when b.last_course_time is null then '' else b.course_name end as last_course_name,c.name as member_name,c.mobile";
		$list=db("GymPtFollow")->alias("a")->field($field)->join([["m_gym_pt_order b","a.pt_order_id=b.id","left"],["m_gym_member c","a.member_id=c.id","left"]])->where("a.pt_id=$pt_id and a.type=1 $search_day and (c.name like '%$keywords%' or c.mobile like '%$keywords%') ")->order("a.id desc")->page($page,10)->select();
		if(empty($list)){
			return $this->api_ajax_error("","抱歉没有更多的数据。。。");
		}else{
			return $this->api_ajax_success($list);
		}
	} 
	
	/**
	 * 私教管理--私教会员
	 * */
	function PrivateMemberList(){
		$pt_id=input("pt_id");
		$keywords=input("keywords");
		$field="b.id as pt_order_id,b.course_name,b.last_course_time,b.price,b.member_id,b.surplus,b.end_date,b.course_count,case when b.last_course_time is null then '' else b.course_name end as last_course_name,c.name as member_name,c.mobile";
		$page=input("page/d",1);
		$list=db("GymPtOrder")->alias("b")->field($field)->join([["m_gym_member c","b.member_id=c.id","left"]])->where("b.pt_id=$pt_id  and (c.name like '%$keywords%' or c.mobile like '%$keywords%') ")->order("b.id desc")->page($page,10)->select();
		if(empty($list)){
			return $this->api_ajax_error("","抱歉没有更多的数据。。。");
		}else{
			return $this->api_ajax_success($list);
		}
	}
	
	/**
	 * 私教管理--预约会员
	 * */
	function OrderMemberList(){
		$pt_id=input("pt_id");
		$keywords=input("keywords");
		$search_day=input("search_day","");
		if(!empty($search_day)){
			$search_day="and date_format(a.order_date, '%Y-%m-%d')='$search_day'";
		}
		$field="a.id as order_id,a.pt_order_id,a.member_id,a.course_status,a.order_date,b.surplus,b.course_name,b.course_count,b.course_name,case when b.last_course_time is null then '' else b.course_name end as last_course_name,c.name as member_name,c.mobile";
		$page=input("page/d",1);
		$list=db("GymPtFollow")->alias("a")->field($field)->join([["m_gym_pt_order b","a.pt_order_id=b.id","left"],["m_gym_member c","a.member_id=c.id","left"]])->where("a.pt_id=$pt_id and a.type=2  $search_day  and (c.name like '%$keywords%' or c.mobile like '%$keywords%') ")->order("a.id desc")->page($page,10)->select();
		if(empty($list)){
			return $this->api_ajax_error("","抱歉没有更多的数据。。。");
		}else{
			return $this->api_ajax_success($list);
		}
	}
	
	/**
	 * 私教管理--课程即将到期
	 * */
	function OverDue(){
		$pt_id=input("pt_id/d");
		$keywords=input("keywords");
		$field="b.id as pt_order_id,b.course_name,b.member_id,b.end_date,b.surplus,b.course_count,case when b.last_course_time is null then '' else b.course_name end as last_course_name,c.name as member_name,c.mobile";
		$page=input("page/d",1);
		$list=db("GymPtOrder")->alias("b")->field($field)->join([["m_gym_member c","b.member_id=c.id","left"]])->where("b.pt_id=$pt_id and ((TO_DAYS(b.end_date)-TO_DAYS(CURDATE()) BETWEEN 0 AND 30) or b.surplus<2)   and (c.name like '%$keywords%' or c.mobile like '%$keywords%') ")->order("b.id desc")->page($page,10)->select();
		if(empty($list)){
			return $this->api_ajax_error("","抱歉没有更多的数据。。。");
		}else{
			return $this->api_ajax_success($list);
		}
	}

	/**
	 * 私教管理--长时间未上课
	 * 购买私教课一次都没有上过 或者 距离上一次上课时间大于45天
	 * */
	function LongTimeGo(){
		$pt_id=input("pt_id");
		$keywords=input("keywords");
		$field="b.id as pt_order_id,b.course_name,b.member_id,b.last_course_time,b.surplus,b.course_count,case when b.last_course_time is null then '' else b.course_name end as last_course_name,c.name as member_name,c.mobile";
		$page=input("page/d",1);
		$list=db("GymPtOrder")->alias("b")->field($field)->join([["m_gym_member c","b.member_id=c.id","left"]])->where("b.pt_id=$pt_id and  last_course_time is not null and (TO_DAYS(last_course_time)-TO_DAYS(CURDATE())>45 ) and (c.name like '%$keywords%' or c.mobile like '%$keywords%') ")->order("b.id desc")->page($page,10)->select();
		if(empty($list)){
			return $this->api_ajax_error("","抱歉没有更多的数据。。。");
		}else{
			return $this->api_ajax_success($list);
		}
	}
	
	/**
	 * 私教管理--添加跟进
	 * */
	function AddFollowUpPost(){
		$data['pt_id']=input("pt_id/d");
		$data['content_way']=input("content_way/d");
		$data['remarks1']=input("remarks1");
		$data['remarks']=input("remarks");
		$data['follow_time']=input("follow_time");
		$data['member_id']=input("member_id/d");
		$data['shop_id']=input("shop_id/d");
		$data['pt_order_id']=input("pt_order_id/d");
		
		if(empty(input("pt_id"))||empty(input("member_id"))||empty(input("shop_id"))||empty(input("pt_order_id"))){
			return $this->api_ajax_error("","参数错误");
		}
		$follow_id=db("GymPtFollow")->insertGetId($data);
		action("PTFollow/after_pt_follow",["pt_order_id"=>input("pt_order_id"),"follow_id"=>$follow_id]);
		return $this->api_ajax_success();
	}
	
	/**
	 * 私教管理--添加预约
	 * */
	function AddOrderPost(){
		$data['pt_id']=input("pt_id/d");
		$data['content_way']=input("content_way/d");
		$data['remarks']=input("remarks");
		$data['member_id']=input("member_id/d");
		$data['shop_id']=input("shop_id/d");
		$data['pt_order_id']=input("pt_order_id/d");
		$data['type']=2;
		$data['order_date']=input("order_date");//预约日期
		$data['order_time']=input("order_time");//预约日期
		$data['course_count']=input("course_count/d");//预约几节课
		if(empty(input("pt_id"))||empty(input("member_id"))||empty(input("shop_id"))||empty(input("pt_order_id"))||empty(input("order_time"))||empty(input("order_date"))){
			return $this->api_ajax_error("","参数错误");
		}
		//预约时间必须小于合同时间
		$is_over_due=db("GymPtOrder")->where("id=".input("pt_order_id")." and end_date>='".input("order_date")."' and start_date<='".input('order_date')."'")->fetchsql(FALSE)->count(1);
		if(empty($is_over_due)){
			return $this->api_ajax_error("","预约时间必须在合同有效期内");
		}
		$this->HasOrder(input("pt_order_id"));
		$follow_id=db("GymPtFollow")->insertGetId($data);
		action("PTFollow/after_pt_order",["pt_order_id"=>input("pt_order_id"),"follow_id"=>$follow_id]);
		return $this->api_ajax_success();
	}
	/**
	 * 是否有预约
	 * */
	function HasOrder($pt_order_id){
		$last_order_time=db("GymPtOrder")->where("id=".$pt_order_id)->value("last_order_time");
		if(!empty($last_order_time)){
			if($last_order_time>date("Y-m-d H:i:s")){
				return $this->api_ajax_error("","已经预约过了，不能再次预约");
			}
		}
	}
	
	/**
	 * 预约详情
	 * */
	function EditOrder(){
		$pt_id=input("pt_id");
		$order_id=input("order_id");
		$field="a.course_count,a.order_date,a.order_time,a.remarks,a.id as order_id,b.mobile,b.name as member_name";
		$model=db("GymPtFollow")->alias("a")->field($field)->join([["m_gym_member b","a.member_id=b.id","left"]])->where("a.id=$order_id and a.pt_id=$pt_id")->find();
		if(empty($model)){
			return $this->api_ajax_error("","pt_id 或者 order_id 错误");
		}else{
			return $this->api_ajax_success($model);
		}
	}
	
	/**
	 * 私教管理--预约修改
	 * */
	function EditOrderPost(){
		$data['remarks']=input("remarks");
		$data['id']=input("order_id/d");
		$data['order_date']=input("order_date");//预约日期
		$data['order_time']=input("order_time");//预约日期
		$data['course_count']=input("course_count/d");//预约几节课
		
		//预约时间必须小于合同时间
		$is_over_due=db("GymPtOrder")->where("id=(select pt_order_id from m_gym_pt_follow where id=".input("order_id/d").") and end_date>='".input("order_date")."' and start_date<='".input('order_date')."'")->fetchsql(FALSE)->count(1);
		if(empty($is_over_due)){
			return $this->api_ajax_error("","预约时间必须在合同有效期内");
		}
		
		if(empty(input("pt_id"))||empty(input("order_id"))){
			return $this->api_ajax_error("","参数错误");
		}
		$follow_id=db("GymPtFollow")->update($data);
		return $this->api_ajax_success();
	}
	/*我的详情*/
	function MyDetail(){
		$pt_id=input("pt_id");
		$shop_id=input("shop_id");
		if(empty($pt_id)||empty($shop_id)){
			return $this->api_ajax_error("","参数错误");
		}
		$model=db("GymEmployee")->field("name as pt_name,id as pt_id,mobile,shop_id")->where("id=$pt_id and type=3 and shop_id=$shop_id")->find();
		if(empty($model)){
			return $this->api_ajax_error("","无效的参数");
		}else{
			return $this->api_ajax_success($model);
		}
	}
	/**
	 * 会员私教课程列表
	 * */
	function  MemberCourseList(){
		$member_id=input("member_id/d");
		$pt_id=input("pt_id/d");
		$shop_id=input("shop_id/d");
		if(empty($pt_id)||empty($shop_id)||empty($member_id)){
			return $this->api_ajax_error("","参数错误");
		}else{
			$list=db("GymPtOrder")->field("id as pt_order_id,course_name")->where("member_id=$member_id and shop_id=$shop_id and pt_id=$pt_id")->select();
			if(empty($list)){
				return $this->api_ajax_error("","抱歉没有更多的数据。。。");
			}else{
				return $this->api_ajax_success($list);
			}
		}
	}
}
